package com.cjb.common.util;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import java.io.StringReader;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;

public class DigitalCertificate {

    private static final String CERTIFICATE_PEM = "-----BEGIN CERTIFICATE-----\n" +
            "MIICKjCCAdCgAwIBAgIUHC5YPWXprEOakdh7M5NyF8q9iI4wCgYIKoZIzj0EAwIw\n" +
            "cDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMQ8wDQYDVQQH\n" +
            "EwZEdXJoYW0xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMTE2Nh\n" +
            "Lm9yZzEuZXhhbXBsZS5jb20wHhcNMjQwMzAyMDY0NTAwWhcNMjUwMzAyMDY1MDAw\n" +
            "WjAhMQ8wDQYDVQQLEwZjbGllbnQxDjAMBgNVBAMTBWFkbWluMFkwEwYHKoZIzj0C\n" +
            "AQYIKoZIzj0DAQcDQgAEjCKneBCo5SkpUnh1s9x4y311iFrQTuv04HbEUIa00dg+\n" +
            "9+CB6L1EpOeJc6jBb6RFVX5Z91gfsuk3u8rn7DHHJ6OBljCBkzAOBgNVHQ8BAf8E\n" +
            "BAMCA6gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQC\n" +
            "MAAwHQYDVR0OBBYEFPimCpIH1rARa2dGHJVTPe9yD3j6MB8GA1UdIwQYMBaAFMFQ\n" +
            "7te/enBZD7y4jabYXlO3XIENMBQGA1UdEQQNMAuCCWxvY2FsaG9zdDAKBggqhkjO\n" +
            "PQQDAgNIADBFAiEAi7Lwt09qK0Qazl4Y5wd9MdQ+DCr+SEqR3RY58DDpUPQCIFk2\n" +
            "9gWB49KsPWhBS//md1iE+xG0WllfJbC2B2URXlHT\n" +
            "-----END CERTIFICATE-----";

    private static final String PRIVATE_KEY_PEM = "-----BEGIN PRIVATE KEY-----\n" +
            "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgOuM/TkJbT3lVEZ7m\n" +
            "D+V0x/A+7FIH6jtYhcpGWCu4yK2gCgYIKoZIzj0DAQehRANCAASMIqd4EKjlKSlS\n" +
            "eHWz3HjLfXWIWtBO6/TgdsRQhrTR2D734IHovUSk54lzqMFvpEVVfln3WB+y6Te7\n" +
            "yufsMccn\n" +
            "-----END PRIVATE KEY-----";

    private Certificate certificate;
    private PrivateKey privateKey;

    public DigitalCertificate(String certificatePem,String privateKeyPem) throws Exception {
        // Add BouncyCastle provider if not already present
        if (Security.getProvider("BC") == null) {
            Security.addProvider(new BouncyCastleProvider());
        }

        // Load the certificate
        PEMParser certParser = new PEMParser(new StringReader(certificatePem));
        X509CertificateHolder certHolder = (X509CertificateHolder) certParser.readObject();
        JcaX509CertificateConverter converter = new JcaX509CertificateConverter();
        this.certificate = converter.getCertificate(certHolder);

        // Load the private key
        PEMParser keyParser = new PEMParser(new StringReader(privateKeyPem));
        Object parsedKey = keyParser.readObject();
        JcaPEMKeyConverter keyConverter = new JcaPEMKeyConverter().setProvider("BC");
        this.privateKey = keyConverter.getPrivateKey((PrivateKeyInfo) parsedKey);
    }

    public Certificate getCertificate() {
        return certificate;
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }
}